﻿using Diary.BAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Diary.Win
{
    public partial class SettingForm : Form
    {
        public SettingForm()
        {
            InitializeComponent();
        }

        
        private void btnVerify_Click(object sender, EventArgs e)
        {

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //口令设置
            if (!string.IsNullOrEmpty(tbxPasswordNew.Text))
            {
                string input = CommonLib.MD5.Encry16(tbxPasswordOld.Text);

                //string save = CommonLib.MD5.Encry16(BAL.Setting.get_string_value("Password"));
                string save = BAL.Setting.get_string_value("Password");
                if (string.IsNullOrEmpty(save))
                {
                    save = CommonLib.MD5.Encry16("123456");
                }
                if (input != save)
                {
                    MessageBox.Show("原口令错误，请重新输入");
                    return;
                }
                string pwd_new = tbxPasswordNew.Text;
                string pwd_confirm = tbxPasswordConfirm.Text;
                if (pwd_new != pwd_confirm)
                {
                    MessageBox.Show("新口令与确认口令不一致，请重新输入");
                    return;
                }

                if (BAL.Setting.update_value("Password", CommonLib.MD5.Encry16(pwd_new)))
                {
                    //lblTip.Text = "新口令已修改！";
                }
                else
                {
                    MessageBox.Show(string.Format("新口令未能成修改！原因：{0}", BAL.DBController.Default.LastException.Message));
                    return;
                }
            }

            //备份设置
            if (!string.IsNullOrEmpty(tbxBackupPath.Text))
            {
                string backup_path = tbxBackupPath.Text;
                if (!string.IsNullOrEmpty(backup_path))
                {
                    if (!System.IO.Directory.Exists(backup_path))
                    {
                        MessageBox.Show("路径不存在");
                        return;
                    }

                    if (!BAL.Setting.update_value("BackupPath", tbxBackupPath.Text))
                    {
                        MessageBox.Show(string.Format("备份路径未能成功修改！原因：{0}", BAL.DBController.Default.LastException.Message));
                        return;
                    }
                }
                if (!BAL.Setting.update_bool_value("AutoBackupAtExit", cbxAutoBackupAtExit.Checked))
                {
                    MessageBox.Show(string.Format("自动备份未能成功修改！原因：{0}", BAL.DBController.Default.LastException.Message));
                    return;
                }
            }

            MessageBox.Show("保存成功");
        }

        private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                tbxBackupPath.Text = dlg.SelectedPath;
            }
        }

        private void btnRunBackup_Click(object sender, EventArgs e)
        {
            if (Global.Default.BackupDB())
            {
                MessageBox.Show("已成功备份");
            }
        }

        private void SettingForm_Load(object sender, EventArgs e)
        {
            tbxBackupPath.Text = BAL.Setting.get_string_value("BackupPath");
            cbxAutoBackupAtExit.Checked = BAL.Setting.get_bool_value("AutoBackupAtExit");
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            if (System.IO.Directory.Exists(tbxBackupPath.Text))
            {
                System.Diagnostics.Process.Start("explorer.exe", tbxBackupPath.Text);
            }
        }

        private void btnRestore_Click(object sender, EventArgs e)
        {
            Global.Default.RestoreDB();
        }
    }
}
